/**

 */
package com.adkfp.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.adkfp.bean.ADKManageRoleBean;
import com.adkfp.bean.ADKManageRolePageBean;
import com.adkfp.bean.ADKManageUserBean;
import com.adkfp.bean.ADKManageUserRoleBean;
import com.adkfp.service.IADKManagePageService;
import com.adkfp.service.IADKManageRoleService;
import com.adkfp.service.IADKManageUserService;
import com.adkfp.utils.LayuiUtil;

/**
 * class name:SelectclassAction <BR>
 * class description: please write your description <BR>
 * Remark: <BR>
 * @version 1.00 2019年1月25日
 * @author ADK)zhoulin
 */
@Controller
@RequestMapping("aDKManageRole")
public class ADKManageRoleAction {
	@Resource(name="aDKManageRoleService")
	private IADKManageRoleService aDKManageRoleService;
	@Resource(name="aDKManageUserService")
	private IADKManageUserService aDKManageUserService;
	@Resource(name="aDKManagePageService")
	private IADKManagePageService aDKManagePageService;
	
	/**
	 * 获取所有页面权限
	 */
	@RequestMapping(value="getAllManagePage")
	@ResponseBody
	public Object getAllManagePage(){
		
		return aDKManagePageService.getAllManagePage();
	}
	
	
	
	/**
	 * 增加角色
	 */
	@RequestMapping(value="addManageRole")
	@ResponseBody
	public String addManageRole(ADKManageRoleBean bean){
		
		try {
			aDKManageRoleService.insert(bean);
			return "SUCCESS";
		} catch (Exception e) {
			return "ERROR";
		}
		
	}
	
	/**
	 * 删除角色
	 */
	@RequestMapping(value="delManageRole")
	@ResponseBody
	public String delManageRole(long id){
		
		try {
			aDKManageRoleService.delManageRoleById(id);
			//删除角色的页面
			aDKManagePageService.deleteManagePageByUserId(id);
			//删除某个角色的所有用户
			aDKManageUserService.deleteUserRoleByRole(id+"");
			
			return "SUCCESS";
		} catch (Exception e) {
			return "ERROR";
		}
	}
	
	/**
	 * 修改角色
	 */
	@RequestMapping(value="updateManageRole")
	@ResponseBody
	public String updateManageRole(long id, String name){
		
		try {
			aDKManageRoleService.updateManageRoleById(id, name);
			return "SUCCESS";
		} catch (Exception e) {
			return "ERROR";
		}
	}
	
	/**
	 * 增加某个角色的权限页面
	 */
	@RequestMapping(value="addManagePageByRole")
	@ResponseBody
	public String addManagePageByRole(long roleId, long[] pageIds){
		
		try {
			
			//先删除老的权限
			aDKManagePageService.deleteManagePageByUserId(roleId);
			
			if(pageIds != null && pageIds.length > 0){
				List<ADKManageRolePageBean> rolePageList = new ArrayList<ADKManageRolePageBean>();
				
				for (long pageId : pageIds) {
					ADKManageRolePageBean rolePage = new ADKManageRolePageBean();
					rolePage.setRoleId(roleId);
					rolePage.setPageId(pageId);
					
					rolePageList.add(rolePage);
				}
				
				aDKManagePageService.addRolesManagePage(rolePageList);
			}
			return "SUCCESS";
		} catch (Exception e) {
			return "ERROR";
		}
		
	}
	
	/**
	 * Method name: toApPage <BR>
	 * Description: 跳转到选择用户页面 <BR>
	 * Remark: <BR>
	 */
	@RequestMapping(value="selectUser")
	public String selectUser(HttpServletRequest request,Integer roleId){
		request.setAttribute("roleId", roleId);
		return "manage/selectUser";
	}
	
	 /**
 	* Method name: selectAll <BR>
 	* Description: 查询所有记录 <BR>
 	* Remark: <BR>
 	* @param bean
 	* @return  Object<BR>
 	*/
	@RequestMapping("selectAll")
	@ResponseBody
	public Object selectAll(ADKManageRoleBean bean) {
		List<ADKManageRoleBean> list = new ArrayList<>();
		list = aDKManageRoleService.findByCondition(bean);
		long count = aDKManageRoleService.findByConditionCount(bean);
		return LayuiUtil.data(count, list);
	}

	@RequestMapping("select")
	@ResponseBody
	public Object select(ADKManageUserBean bean,Integer id) {
		System.out.println("aaaaaaa--------------"+id);
		List<ADKManageUserBean> list = new ArrayList<>();
		bean.setId(id);
		list = aDKManageUserService.afindByCondition(bean);
		long count = aDKManageUserService.afindByConditionCount(bean);
		return LayuiUtil.data(count, list);
	}
	/**
	 * Method name: index <BR>
	 * Description: 转发到页面 <BR>
	 * Remark: <BR>
	 * @return  String<BR>
	 */
	@RequestMapping("index")
	public String index() {
		return "manage/RoleTT";
	}
	
	@RequestMapping("index1")
	public String index1() {
		return "manage/addrole";
	}
	
	@RequestMapping("index2")
	public String index2(HttpServletRequest request,String id,ADKManageRoleBean bean) {
		System.out.println("zzzzzzzzzzz------------"+id);
		request.setAttribute("u",bean);
		return "manage/selectUser";
	}
	
	/**
	 * Method name: insertNetGroup <BR>
	 * Description: 增加/修改 <BR>
	 * Remark: <BR>
	 * @param teachingLogBean  void<BR>
	 */
	/*@RequestMapping("insert")
	@ResponseBody
	public void insert(HttpServletRequest request,ADKManageRoleBean bean) {
		if(bean.getId()!=null&&bean.getId()!=0) {
			aDKManageRoleService.update(bean);
		}else {
			aDKManageRoleService.insert(bean);
		}
	}*/
	
	//用于修改页面(查找id)
	/*@RequestMapping("findId")
	public String findId(HttpServletRequest request,String id,ADKManageRoleBean bean) {
		if(id != null && !id.equals("")) {
			bean=aDKManageRoleService.findById(Integer.parseInt(id));
		}
		request.setAttribute("u",bean);
		return "manage/addrole";
	}
	*/
	@RequestMapping("findId2")
	public Object findId2(HttpServletRequest request,Integer id,ADKManageRoleBean bean) {
		System.out.println("呵呵呵呵呵----------------"+id);
		bean.setId(id);
		request.setAttribute("u",bean);
		return "manage/rolelist";
	}
	
	//删除
	/*@RequestMapping("delete")
	@ResponseBody
	public String delete(String id) {
		System.out.println("----id----"+id);
		if(id!=null&&!id.equals("")&&!id.equals(1)) {
			aDKManageRoleService.delete(Integer.parseInt(id));
		return "SUCCESS";
	  }else {
		return "ERROR";
		}
	}*/
	
	// 删除用户的角色
	@RequestMapping(value="deleteUserRole")
	@ResponseBody
	public String deleteUserRole(int roleId, int userId){
		try {
			aDKManageUserService.deleteUserRole(userId+"", roleId+"");
			return "SUCCESS";
		} catch (Exception e) {
			return "ERROR";
		}
	}
	
	/**
	 * 获得所有角色
	 */
	@RequestMapping(value="getAllManageRole")
	@ResponseBody
	public Object getAllManageRole(){
		System.out.println("-----------------aaaaaaa----------------");
		//所有角色
		Map<String, String> map = new HashMap<String, String>();
		map.put("deleteFlag", "0");
		List<ADKManageRoleBean> roleList = aDKManageRoleService.getManageRoleByMap(map);
		
		return roleList;
	}
	
	/**
	 * 获取某个角色的权限页面
	 */
	@RequestMapping(value="getManagePageByRole")
	@ResponseBody
	public Object getManagePageByRole(long roleId){
		
		return aDKManagePageService.getManagePageByUserId(roleId);
	}
	
	/**
	 * 增加用户的角色
	 */
	@RequestMapping(value="addUserRole")
	@ResponseBody
	public String addUserRole(int roleId, int[] userIds){
		
		try {
			if(userIds != null && userIds.length > 0){
				for (int userId : userIds) {
					
					ADKManageUserRoleBean manageUserRole = new ADKManageUserRoleBean();
					manageUserRole.setRoleId(roleId);
					manageUserRole.setUserId(userId);
					
					aDKManageUserService.insertjuese(manageUserRole);
					
				}
			}
			return "SUCCESS";
		} catch (Exception e) {
			return "ERROR";
		}
		
	}
	
}
